Next | Prev | Up | Top | Contents | Index

Barriers

A barrier is a memory object that represents a point of rendezvous between multiple processes. You use a barrier to ensure that processes do not advance until some necessary preparation has been done.

A barrier is created by newbarrier() in an arena built by usinit(). The barrier is used by some fixed number (N) of processes. When each process is ready to rendezvous with the others, it issues barrier(N). As each process arrives at the barrier, it is suspended. When the Nth process calls barrier(), all the processes resume execution.

A barrier is the computing equivalent of N coworkers who agree to go to lunch together. When each person realizes it is lunch time, he or she goes to the lobby. When the Nth coworker reaches the lobby, all of them depart together for lunch. A barrier is very useful in initializing an application based on the Frame Scheduler (see "Preparing the System").

As an example of the use of a barrier, imagine that you discover that a nested loop to take the sum of a large matrix is a bottleneck in your program. To speed up the calculation you divide it between two processes. (Presumably they will run in different CPUs.) The first process is the one that requires the matrix sum, and which originally calculated the sum by itself. The second process is a new one, whose only purpose is to assist in the matrix sum calculation. You create a barrier named matsum to coordinate the two.

The logic of the second, helper, process is as follows:

  1. Call barrier(matsum,2) to wait until it is time to take the sum.

  2. Calculate the sum over all even-numbered rows of the matrix.

  3. Store the sum in global evensum.

  4. Call barrier(matsum,2) to wait until the first process is finished.

  5. Return to step 1.
The logic of the first, main process would be as follows:

  1. Perform other work as required until the matrix sum is needed.

  2. Call barrier(matsum,2) to release the helper process.

  3. Calculate the sum over all odd-numbered rows of the matrix.

  4. Call barrier(matsum,2) to wait until the second process has finished its calculation.

  5. Add evensum to the odd total to get the grand total.

  6. Return to step 1.
The example can be generalized to more processes, and to any other calculation that can be partitioned in this way.


Next | Prev | Up | Top | Contents | Index